package cn.itcast.controller;

import cn.itcast.domain.kefang;
import cn.itcast.domain.kehu;
import cn.itcast.service.impl.kefangService;
import cn.itcast.service.impl.kehuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(path="/find")
public class Find{
    @Autowired
    private kehuService kehuservice;
    @Autowired
    private kefangService kefangservice;
//通过客户id找详细信息，用于find_kehu.jsp
    @RequestMapping(path="/kehuByName")
    public String kehuByName(String Id_num,String name,Model model) {
        kehu kh ;
        if(!Id_num.isEmpty()){
            kh = kehuservice.findkehuById(Id_num);
        }else{
            kh = kehuservice.findkehuByName(name);
        }
        model.addAttribute("kehu",kh);
//        model.addAttribute("name", kh.getName());
//        model.addAttribute("Id_num", kh.getId_num());
//        model.addAttribute("InTime", kh.getInTime());
//        model.addAttribute("OutTime", kh.getOutTime());
//        model.addAttribute("Num", kh.getNum());

        return "find_kehu";
    }
//    通过状态来查找客房以供入住选择,用于find/kefangByStatue
    @RequestMapping(path = "/kefangByStatue")
    public String kefangByStatue(Model model){
        List<kefang> list = kefangservice.kefangByStatue();
        model.addAttribute("list",list);
        return "select_kefang";
    }
//    专用于kehu_update.jsp，先获得信息，跳到详细界面后修改
    @RequestMapping("/kehuById")
    public String kefangByNum(kehu kh,Model model){
        String name= kh.getName();
        String Id_num = kh.getId_num();
        kehu keHu;
        if(!name.isEmpty()){
            keHu = kehuservice.findkehuByName(name);
        }else{
            keHu = kehuservice.findkehuById(Id_num);
        }

        model.addAttribute("name",keHu.getName());
        model.addAttribute("Id_num",keHu.getId_num());
        model.addAttribute("num",keHu.getNum());
        model.addAttribute("InTime",keHu.getInTime());

        return "kehu_update";
    }
//    获得num，显示客房信息，用于User_index.jsp
    @RequestMapping("/kefangByNum_Find")
    public String kefangByNum_Find(Integer num,Model model){
        kehu kh = kehuservice.findkehuByNum(num);
        kefang kf = kefangservice.findkefangByNum(num);
        model.addAttribute("num",kf.getNum());
        model.addAttribute("type",kf.getType());
        if(kf.getStatue()!=0){
            model.addAttribute("name",kh.getName());
        }else{
            model.addAttribute("name","空房，无人");
        }
        return "query_kefang";
    }
//    寻找所有未退房的客户，跳至cancel_kefang.jsp，用于find/kehuByStatue
    @RequestMapping("/kehuByStatue")
    public String kehuByStatue(Model model){
        List<kehu> list = kehuservice.findAllkehu();
        model.addAttribute("list",list);
        return "cancel_kefang";
    }
}
